#include "heroz.hh"
#include "guru_meditation.h"
#include "fmemory.h"

#define TITLE_DISPLAY_LINE 1000
#define TITLE_SELECT_LINE 2000

bool title(HeroZ *heroz) {
  SDL_Event event;

  heroz->basic->run(TITLE_DISPLAY_LINE); //Title Screen
  SDL_Flip(heroz->display);
  while(SDL_WaitEvent(&event) == 1) {
    bool function_selected = true;
    switch(event.type) {
    case SDL_QUIT:
      return false;
      break;
    case SDL_KEYDOWN:
      //printf("state=%d sym=$%04X\n", event.key.state, event.key.keysym.sym);
      switch(event.key.keysym.sym) {
      case SDLK_ESCAPE:
	return false;
	break;
      case SDLK_F1:
	heroz->basic->var_int("FUNCTION") = 1;
	break;
      case SDLK_F2:
	heroz->basic->var_int("FUNCTION") = 2;
	break;
      case SDLK_F3:
	heroz->basic->var_int("FUNCTION") = 3;
	break;
      case SDLK_F4:
	heroz->basic->var_int("FUNCTION") = 4;
	break;
      case SDLK_F5:
	heroz->basic->var_int("FUNCTION") = 5;
	break;
      case SDLK_F6:
	heroz->basic->var_int("FUNCTION") = 6;
	break;
      case SDLK_F7:
	heroz->basic->var_int("FUNCTION") = 7;
	break;
      case SDLK_F8:
	heroz->basic->var_int("FUNCTION") = 8;
	break;
      default:
	break;
      }
      break;
    default:
      function_selected = false;
    }
    if(function_selected) {
      heroz->basic->run(TITLE_SELECT_LINE);
      return true;
    }
  }
  int mb = guru_meditation(GM_FLAGS_RECOVERY | GM_FLAGS_CHOICE, GM_SS_WorkBench | GM_GE_NoSignal, (void*)&title);
  return mb == 1;
}




int main() {
  HeroZ *heroz;

  heroz = init_engine("data");

  //We finished with initialisation, now run the code
  heroz->basic->run();
  play_the_game(heroz);
  while(heroz->basic->var_int("RUNGAME")) {
    if(!title(heroz)) break;
  }
  // init_sound();
  // load_font();
  // run_basic();
  clean_up(heroz);
  ffree(heroz);
  return 0;
}
